package com.gph.saviorframework.security.service.impl;

import com.gph.saviorframework.FieldType;
import com.gph.saviorframework.common.model.Org;
import com.gph.saviorframework.dao.security.OrgDao;
import com.gph.saviorframework.security.service.OrgService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class OrgServiceImpl implements OrgService {

    @Autowired
    private OrgDao orgDao;

    private Map<String, String> fields = new HashMap<String, String>();

    public OrgServiceImpl(){
        fields.put("ORG_ID", FieldType.STRING);
        fields.put("ORG_NAME",FieldType.STRING);
    }

    public List<Map<String, Object>> find(String parent, String sort, String dir, Map<String, Object> params) {
        return orgDao.find(sort,dir,genWhereClause(params,parent));
    }

    public long count(String parent, Map<String, Object> params) {
        return orgDao.count(genWhereClause(params,parent));
    }

    public List<Map<String, Object>> findByParent(String parent) {
        return orgDao.findByParent(parent);
    }

    @Override
    public List<Org> findByParent2(String parent) {
        return orgDao.findByParent2(parent);
    }

    public List<Map<String, Object>> findByParentEasyUI(String parent) {
        return orgDao.findByParentEasyUI(parent);
    }

    public List<Map<String, Object>> findByRole(String parent, String role) {
        return orgDao.findByRole(parent,role);
    }

    public List<Map<String, Object>> findByPosition(String parent, String position) {
        return orgDao.findByPosition(parent,position);
    }

    /**
     * get key
     *
     * @return
     */
    @Override
    public String getKey() {
        return orgDao.getKey();
    }

    @Override
    public boolean checkOrgRelPosition(String orgId, String positionId) {
        return orgDao.checkOrgRelPosition(orgId,positionId);
    }

    @Override
    public List<String> getAllIds() {
        return orgDao.getAllIds();
    }

    /**
     * 保存
     *
     * @param org
     */
    public void save(Org org) {
        if(org.getParent()!=null){
            org.setPath(org.getParent().getPath() + "|" + org.getId());
        }
        if(org.getPath().contains("|")){
            org.setLevel(org.getPath().split("\\|").length);
        }
        else{
            org.setLevel(1);
        }
        orgDao.save(org);
    }

    /**
     * 根据编号获取
     *
     * @param id
     * @return
     */
    public Org get(String id) {
        return orgDao.get(id);
    }

    /**
     * 更新
     *
     * @param org
     * @return
     */
    public void update(Org org) {
        orgDao.update(org);
    }

    /**
     * 批量更新
     *
     * @param orgs
     */
    @Override
    public void update(Set<Org> orgs) {
        for(Org org : orgs){
            update(org);
        }
    }

    /**
     * 根据条件返回数据
     *
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params) {
        return find(null,sort,dir,params);
    }

    /**
     * 带分页参数的，给bootstrap使用的
     *
     * @param start  开始页码，从1开始
     * @param limit  每页查询的次数
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> find(int start, int limit, String sort, String dir, Map<String, Object> params,Object... extras) {
        return null;
    }

    /**
     * 统计数量
     *
     * @param params
     * @return
     */
    public long count(Map<String, Object> params) {
        return count(null,params);
    }

    /**
     * 根据主键删除
     *
     * @param id
     */
    public void delete(String id) {
        orgDao.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void deleteByIds(String[] ids) {
        for(String id : ids){
            delete(id);
        }
    }

    /**
     * 查询字段集合
     *
     * @return
     */
    public Map<String, String> getFields() {
        return fields;
    }

    /**
     * 根据id查询map
     *
     * @param id
     * @return
     */
    public Map<String, Object> findById(String id) {
        return orgDao.findById(id);
    }

    /**
     * 生成where条件
     *
     * @param params
     * @param extra
     * @return
     */
    public String genWhereClause(Map<String, Object> params, Object... extra) {
        String whereClause = "1=1";
        if(extra.length==1){
            String parent = extra[0].toString();
            if(!StringUtils.isEmpty(parent)){
                whereClause+=" AND O.PARENT_ORG_ID='"+parent+"'";
            }
        }

        if (params!=null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                if (fields.get(key).equals(FieldType.STRING)) {
                    whereClause += " AND O." + key + " LIKE '"+params.get(key)+"'";
                } else {
                    whereClause += " AND O." + key + " ='"+params.get(key)+"'";
                }
            }
        }
        return whereClause;
    }

    @Override
    public Map<String, String> getIdNames(){
        List<Map<String, String>> list = orgDao.getIdNames();
        Map<String, String> reMap = new HashMap<String, String>();
        for (Map<String, String> map : list) {
            reMap.put(map.get("id").toString(), map.get("name").toString());
        }
        return reMap;
    }
}
